using UnityEngine;
using System.Collections.Generic;
using NodeEditorFramework.Utilities;
using UnityEditor;

namespace NodeEditorFramework.SkillComposer
{
	[Node(false, "Skill/Format")]
	public class FormatNode : Node
	{
		public const string ID = "FormatNode";
		public override string GetID { get { return ID; } }

		public override Vector2 MinSize { get { return new Vector2(200, 10); } }
		public override bool AutoLayout { get { return true; } } // IMPORTANT -> Automatically resize to fit list

		private ValueConnectionKnobAttribute dynaCreationAttribute = new ValueConnectionKnobAttribute("Output", Direction.Out, DataType.String);

		private string[] options = new string[] { DataType.String, DataType.Float, DataType.Format};
		private int selectedIndex = 0;

		[ValueConnectionKnob("out", Direction.Out, DataType.Format)]
		public ValueConnectionKnob outputKnob;

		public override void NodeGUI()
		{
			GUILayout.Label("This node resizes to fit all inputs!");

			// Display text field and add button
			GUILayout.BeginHorizontal();
			//newLabel = RTEditorGUI.TextField(GUIContent.none, newLabel);
			selectedIndex = RTEditorGUI.Popup(GUIContent.none, selectedIndex, options);
			if (GUILayout.Button("Add", GUILayout.ExpandWidth(false)))
			{
				dynaCreationAttribute = new ValueConnectionKnobAttribute("Input", Direction.In, options[selectedIndex]);
				CreateValueConnectionKnob(dynaCreationAttribute);
			}
			GUILayout.EndHorizontal();

			for (int i = 0; i < dynamicConnectionPorts.Count; i++)
			{ // Display label and delete button
				GUILayout.BeginHorizontal();
				GUILayout.Label((i + 1).ToString());
				GUILayout.Label((dynamicConnectionPorts[i] as ValueConnectionKnob).valueType.ToString());
				((ValueConnectionKnob)dynamicConnectionPorts[i]).SetPosition();
				if(GUILayout.Button("x", GUILayout.ExpandWidth(false)))
				{ // Remove current label
					DeleteConnectionPort(i);
					i--;
				}
				GUILayout.EndHorizontal();
			}

			outputKnob.DisplayLayout();
		}

		public override object OutData()
		{
			var data = new List<object>();

            for (int i = 0; i < dynamicConnectionPorts.Count; i++)
            {
				if (dynamicConnectionPorts[i].connected())
                {
					AddConnection(data, dynamicConnectionPorts[i] as ValueConnectionKnob);
				}
            }

			return data;
		}

		public static void AddConnection(List<object> data, ValueConnectionKnob item)
		{
			foreach (var connection in item.connections)
			{
				data.Add(connection.body.OutData());
			}
		}
	}
}